package svc

import (
	"fmt"
	"github.com/zeromicro/go-zero/rest"
	"gorm.io/driver/mysql"
	"gorm.io/gorm"
	"log"
	"ruoyi_zero/internal/config"
	"ruoyi_zero/internal/middleware"
	"ruoyi_zero/utils"
)

type ServiceContext struct {
	Config     config.Config
	DB         *gorm.DB
	CheckLogin rest.Middleware
}

func getDB() *gorm.DB {
	dsn := fmt.Sprintf("%s:%s@(%s:%d)/%s?charset=utf8mb4&parseTime=True&loc=Local", "root", "Ab123456", "192.168.0.207", 3307, "ry-vue")
	db, err := gorm.Open(mysql.Open(dsn))
	if err != nil {
		log.Fatalln("[DB ERROR] : ", err)
	}
	//err = db.AutoMigrate(
	//	system.SysDept{},
	//	system.SysConfig{},
	//	system.SysMenu{},
	//	system.SysUser{},
	//	system.SysUserRole{},
	//)
	//if err != nil {
	//	log.Fatalln("[DB ERROR] : ", err)
	//}
	return db
}

func NewServiceContext(c config.Config) *ServiceContext {

	utils.MysqlInit(c)
	return &ServiceContext{
		Config:     c,
		DB:         utils.MysqlDb,
		CheckLogin: middleware.NewCheckLoginMiddleware().Handle,
	}
}
